package gameLogic

import (
	"math/rand"
	"unsafe"

	"github.com/sirupsen/logrus"
)

func Str2bytes(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}

func Bytes2str(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

func IsInProb(Prob float64) bool {
	randRes := rand.Float64()
	return (0 <= randRes && randRes < Prob)
}

func GetRandIdx(Len int) int {
	return rand.Intn(Len)
}

//核心：游戏战斗逻辑, attacker攻击defender
//暂时制作了平A，不涉及技能计算
func AttackLogic(attacker *BaseProps, defender *BaseProps) {
	phyDamage := getPhyDamage(attacker.Attack, defender.PhyDefence)
	logrus.Infof("damage:%d", phyDamage)
	defender.Hp -= int64(phyDamage)
}

//平A物理伤害计算,下面的公式是LOL的护甲计算
func getPhyDamage(attack int64, PhyDefence int64) int64 {
	logrus.Infof("damage:%d, def:%d", attack, PhyDefence)
	damage := float64(attack) * (float64(PhyDefence) / float64(PhyDefence+100))
	if damage == 0 {
		return 1 //至少造成一点伤害
	}
	return (int64(damage))
}

//判断攻击速度
func IsCanAttack(props *BaseProps, epoch uint64) bool {
	if props.Speed == 0 {
		//默认攻击速度为20epoch一次
		props.Speed = 2 * (1000 / kMinTimeMs)
	}
	//logrus.Infof("speed:%d, epoch:%d", props.Speed, epoch)
	return epoch%uint64(props.Speed) == 0
}
